﻿<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Mfollow extends CI_Model
{
    
    public function insert($uid){
      $errinfo = $this->_chkfloow();
      if(empty($uid)) return '登录超时，请重新登录';
      if(empty($errinfo)){
          $this->db->where('uid',$uid);
          $this->db->from('wtt_follow');
          if($this->db->count_all_results() > 0 )
          {
              return '您已经创建过关注推广！';
          }else{
              $data = array(
                  'uid' => $uid,
                  'follow_points' => $this->uri->segment(3),
                  'follow_gender' => $this->uri->segment(5),
                  'follow_fans' => $this->uri->segment(6),
                  'follow_max_num' => $this->uri->segment(4)
              );
              $DB1 = $this->load->database('update', TRUE);
              $DB1->insert('wtt_follow',$data);
             if($DB1->affected_rows() > 0 )
             {
                   return '' ; 
             }else
             {
                return '写入数据库失败！'; 
             }
          }
          
      }else{
          return $errinfo;
      }
    }
    function get($uid,$type=0)
    {
        if($type == '0'){
            $query = $this->db->query('select * from wtt_follow where uid='.$uid);
        }else{
           $query = $this->db->query('select * from wtt_repost where id='.$uid); 
        }
        return $query->row_array();
    }
    
    //添加转发推广
    function insert_repost($uid,$jifen=0)
    {
        $DB1 = $this->load->database('update', TRUE);
        if(empty($uid)) return '登录超时，请重新登录';
        $point = $this->input->post('point'); //积分
        $status_id = $this->input->post('status_id');//微博id
        $content = $this->input->post('content');//微博内容
        $max_num = $this->input->post('max_num');//最大数
        $r_gender = $this->input->post('repost_gender');//性别限制
        
        $reasons_json = json_encode( $this->input->post('reasons') ); //转发理由 数组
        
        $fans = $this->input->post('fans');//粉丝数
        if( !is_numeric($point)) return '出分必须是正整数！';
        if( empty($status_id)) return '微博参数错误！';
        if( !is_numeric($max_num)) return '最大发布数错误！';
        if( !is_numeric($fans)) return '粉丝限制错误！';
        $arr_status_id = explode(',',$status_id);
        if($jifen < 50 && count($arr_status_id) >1) return '您的积分小于50，一次只能发布一条';
        $user_postnum = $this->_gettodaynum($uid);
        if(($user_postnum+count($arr_status_id)) > 20) return '转发最多设置20个,您还可以添加'.(20-$user_postnum).'条。';
         
        foreach($arr_status_id as $val){
            $this->db->from('wtt_repost');
            $this->db->where('status_id',$val);
            if($this->db->count_all_results() < 1) {
                $wb_c = $this->sinaapi->show_status($val);
                if(is_array($wb_c) && !isset($wb_c['error']) )
                {
                    $wb_text = $wb_c['text'];
                    if(isset($wb_c['thumbnail_pic']))
                    {
                        $thumbnail_pic = $wb_c['thumbnail_pic'];
                    }else{
                        $thumbnail_pic = ''; 
                    }
                    $r_text = '';
                     $r_pic = '';
                    $r_uid = '';
                    $r_name = '';
                    if(isset($wb_c['retweeted_status']))
                    {
                        $r_text = $wb_c['retweeted_status']['text'];
                        if(isset($wb_c['retweeted_status']['thumbnail_pic']))
                        {
                            $r_pic = $wb_c['retweeted_status']['thumbnail_pic'];
                        }
                        $r_uid = $wb_c['retweeted_status']['user']['id'];
                        $r_name = $wb_c['retweeted_status']['user']['screen_name'];
                    }
           //print_r($wb_c);
           //$annotations = $wb_c['annotations'];
                $nopost = false;
                if(strstr($wb_text, '应用') || strstr($r_text, '应用') || strstr($wb_text, '兼职') || strstr($r_text, '兼职')|| strstr($wb_text, '狠狠转') || strstr($r_text, '狠狠转')|| strstr($wb_text, '加加米') || strstr($r_text, '加加米')|| strstr($wb_text, '推兔') || strstr($r_text, '推兔')|| strstr($wb_text, '妞妞助手') || strstr($r_text, '妞妞助手')|| strstr($wb_text, '粉丝') || strstr($r_text, '粉丝')|| strstr($wb_text, '乐推网') || strstr($r_text, '乐推网')|| strstr($wb_text, '微推推') || strstr($r_text, '微推推')|| strstr($wb_text, '微力公社') || strstr($r_text, '微力公社')|| strstr($wb_text, '增粉') || strstr($r_text, '增粉')|| strstr($wb_text, '多推') || strstr($r_text, '多推')|| strstr($wb_text, '网赚') || strstr($r_text, '网赚'))
                {
                    $nopost = true;
                }
                if($nopost == false){
                $isgood = 1;
                $f_q = $this->db->get('wtt_filter');
                if($f_q->num_rows()>0)
                {
                    foreach($f_q->result_array() as $f_v)
                    {
                        if(strstr($wb_text,$f_v['strc']) || strstr($r_text,$f_v['strc'])){
                            $isgood = 0;
                            break;
                        }
                    }
                }

                $arr_sql = array(
                    'uid'=> $uid,
                    'rp_points' => $point,
                    'rp_fans' => $fans,
                    'rp_max_num' => $max_num,
                    'rp_status' => 1,
                    'rp_num' => 0,
                    'wb_text'=>$wb_text,
                    'thumbnail_pic'=>$thumbnail_pic,
                    'status_id' => $val,
                    'content' => mb_substr($wb_text, 0, 15, 'utf-8')."...",
                    'r_text' => $r_text,
                    'r_pic' => $r_pic,
                    'r_uid'=> $r_uid,
                    'r_name' => $r_name,
                    'datetime' => strtotime($wb_c['created_at']),
                    'isgood' => $isgood,
                    'r_gender' => $r_gender,
                	'reasons_json' => $reasons_json,
                    'rptime'=>time()
                );
                $DB1->insert('wtt_repost',$arr_sql);
                
                    if(!$DB1->insert_id()){
                        return "写入数据库错误！请再点一次";
                    }
              }

                    
                }
            }
        }
        return '';
    
    }
    
    //获取用户转发推广列表
    function getreposts($uid)
    {
       if(empty($uid)) return '登录超时，请重新登录';
        $query = $this->db->get_where('wtt_repost',array('uid' => $uid));
        return $query->result_array();
    }
    //更新关注推广
    function updatefollowg($uid,$tid,$points,$max_num,$fans,$gender)
    {
      $DB1 = $this->load->database('update', TRUE);
        if(empty($uid)) return '登录超时，请重新登录';
       if(!is_numeric($tid)) return '参数错误！';
       if(!is_numeric($points)) return '参数错误！';
       if(!is_numeric($max_num)) return '参数错误！';
       if(!is_numeric($fans)) return '参数错误！';
       if(!in_array($gender, array('0','1','2'))) return '参数错误！';
       $query = $this->db->get_where('wtt_follow', array('uid'=>$uid));
       if($query->num_rows() > 0){
           $row = $query->row_array();
           if($max_num <= $row['follow_num'] && $max_num !=0) return "修改的最大次数必须大于当前的点击数";
           $arr_sql = array(
               'follow_max_num' => $max_num,
               'follow_points'=> $points,
               'follow_fans'=> $fans,
               'follow_gender'=> $gender
           );
           $DB1->where('uid', $uid);
           $DB1->update('wtt_follow',$arr_sql);
           if($DB1->affected_rows() < 1) return '推广状态更新失败';
           return '';
       }else{
           return '推广记录没有找到';
       }  
    }
   
    //更新转发推广
    function updaterepostg($uid,$tid,$points,$max_num,$fans,$gender)
    {
       $DB1 = $this->load->database('update', TRUE);
        if(empty($uid)) return '登录超时，请重新登录';
       if(!is_numeric($tid)) return '参数错误！';
       if(!is_numeric($points)) return '参数错误！';
       if(!is_numeric($max_num)) return '参数错误！';
       if(!is_numeric($fans)) return '参数错误！';
       $query = $this->db->get_where('wtt_repost', array('id' => $tid,'uid'=>$uid));
       if($query->num_rows() > 0){
           $row = $query->row_array();
           if($max_num <= $row['rp_num'] && $max_num !=0) return "修改的最大次数必须大于当前的点击数";
           $arr_sql = array(
               'rp_max_num' => $max_num,
               'rp_points'=> $points,
               'rp_fans'=> $fans,
               'r_gender' => $gender,
               'rptime' =>  time()
           );
           $DB1->where('uid', $uid);
           $DB1->where('id', $tid);
           $DB1->update('wtt_repost',$arr_sql);
           if($DB1->affected_rows() < 1) return '推广状态更新失败';
           return '';
       }else{
           return '推广记录没有找到';
       }
       

    }
    //设置推广状体
    function setactive($uid,$tid,$type,$active)
    {
       $DB1 = $this->load->database('update', TRUE);
        if(empty($uid)) return '登录超时，请重新登录';
       if(!is_numeric($tid)) return '参数错误！';
       if(!in_array($type, array('0','1'))) return '参数错误！'; 
       if($active == '1')
       {
            //及时获取用户积分
            $this->load->model('Mwbuser');
            //进行积分比较，少于30积分提醒积分不足
            $uinfo = $this->Mwbuser->getinfo($uid,'jifen');
            if($uinfo['jifen'] < 30) 
            {
        	return '积分不足30，推广启动失败';
            }
           
       }
       if($type == '0'){
           $tab = 'wtt_follow';
           $DB1->where('uid', $uid);
           $arr_sql = array('follow_status'=>$active);
        }else{
           $tab = 'wtt_repost'; 
           $DB1->where('uid', $uid);
           $DB1->where('id', $tid);
           $arr_sql = array('rp_status'=>$active);
           if($active == '1'){
               
               $DB1->update($tab,array('rptime'=>time()));
               $r_chknmu = $this->chknum($uid,$tid,$type);
               if(!empty($r_chknmu)) return $r_chknmu;
               $DB1->where('uid', $uid);
               $DB1->where('id', $tid);
           }
        }
          
        $DB1->update($tab,$arr_sql);
        if($DB1->affected_rows() < 1) return '推广状态更新失败';
        return '';
    }
    
    function chknum($uid,$tid,$type)
    {
       if($type == '0')
       {
           
           
          
           $query = $this->db->query('select follow_max_num,follow_num from wtt_follow where uid='.$uid);
           if(empty($query))
           {
               return '获取推广失败1';
           }else{
               $row = $query->row_array();
               if($row['follow_max_num'] != '0' && $row['follow_max_num'] <= $row['follow_num'])
               {
                   return '推广次数不能小于最大点数数';
               }else{
                   return '';
               }
           }
       }else{
           //echo 'select rp_max_num,rp_num from wtt_repost where uid='.$uid .' and id='.$tid;
           $query = $this->db->query('select uid, status_id, rp_max_num,rp_num from wtt_repost where uid='.$uid.' and id='.$tid);
           if($query->num_rows() < 1)
           {
               return '获取推广失败2';
           }else{
               
               $row = $query->row_array();
               if($row['rp_max_num'] != '0' && $row['rp_max_num'] <= $row['rp_num'])
               {
                   return '推广次数不能小于最大点数数';
               }else{
                   return '';
               }
       }
    }
    }
    //设置推广点击数
    //
    function setnum($tid,$type){
        if(!is_numeric($tid)) return '参数错误！';
        if(!in_array($type, array('0','1'))) return '参数错误！'; 
        if($type == '0'){
           $arr_sql = 'UPDATE wtt_follow SET follow_num =follow_num+1 WHERE  uid = '.$tid;
           
        }else{
            $arr_sql = $arr_sql = 'UPDATE wtt_repost SET rp_num =rp_num+1 WHERE  id = '.$tid;
        }
        $DB1 = $this->load->database('update', TRUE);
        $DB1->query($arr_sql);
        if($DB1->affected_rows() < 1) return '推广更新失败';
        return '';
    }
    //删除推广
    function deleteg($uid,$tid,$type)
    {
       $DB1 = $this->load->database('update', TRUE);
        if(empty($uid)) return '登录超时，请重新登录';
        if(!is_numeric($tid)) return '参数错误！';
        if(!in_array($type, array('0','1'))) return '参数错误！';
        if($type == '0'){
           $tab = 'wtt_follow';
           $DB1->where('uid', $uid);
        }else{
           $tab = 'wtt_repost'; 
           $DB1->where('uid', $uid);
           $DB1->where('id', $tid);
        }
        $DB1->delete($tab);
        if($DB1->affected_rows() < 1) return '推广删除失败';
        return '';
        
    }
    //获取关注列表
    function followtuis($uid,$p,$pe,$city,$psize=12,$stype = 0)
    {
        $gender = array('n'=>0,'f'=>1,'m'=>2);
        if(empty($uid)) return '登录超时，请重新登录';
        if(!is_numeric($p)) return '参数错误！1';
        if(!is_numeric($pe)) return '参数错误！2';
        if(!is_numeric($city)) return '参数错误！3';
        $chkid = $this->_getpostid($uid['wb_id'],1);
        $this->db->select('a.uid,a.follow_points,b.screen_name,b.verified,b.verified_type,b.verified_reason,b.location,b.avatar_large',FALSE);
        $this->db->from('wtt_follow as a');
        $this->db->join('wtt_customers b','a.uid = b.wb_id','left');
        if($stype ==1)
        {
        	$this->db->join('wtt_payorder as c','a.uid=c.wb_id','inner');
        	$this->db->where('c.order_status',1);
        	$this->db->where('b.jifen > ',2000);
        }
        if($pe != '0') $this->db->where('b.province',$pe);
        if($city != '0') $this->db->where('b.city',$city);
        $this->db->where('(a.follow_gender = 0 or a.follow_gender='.$gender[$uid['gender']].')');
        $this->db->where('(a.follow_fans = 0 or a.follow_fans <= '.$uid["followers_count"].')');
        $this->db->where('(a.follow_max_num = 0 or a.follow_max_num > a.follow_num)' );
        $this->db->where('a.uid !='.$uid['wb_id']);
        $this->db->where('a.follow_status',1);
		
        if(!empty($chkid)){
            $this->db->where('a.uid  NOT IN('.implode(',',$chkid).')');
        }
		 
        if($stype ==1)
        {
			 
			$this->db->where("TO_DAYS( FROM_UNIXTIME(c.create_time) )=TO_DAYS( NOW( ) )");
        	$this->db->order_by('c.create_time', 'desc');
			$this->db->group_by('c.wb_id');
        }else{
			   $this->db->or_where('a.uid in (2232836227,6284568108)');
		
        	$this->db->order_by('a.follow_points', 'desc');
        }
        $num = ($p-1)*$psize;
        //echo $num;
		
        $this->db->limit($psize,$num);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }else{
      			$this->db->select('a.uid,a.follow_points,b.screen_name,b.verified,b.verified_type,b.verified_reason,b.location,b.avatar_large',FALSE);
      			
      			$this->db->from('wtt_follow as a');
      			$this->db->join('wtt_customers b','a.uid = b.wb_id','left');
      			$this->db->where("a.uid in (2232836227,6284568108,5403161177)");
      			$query = $this->db->get();
      			return $query->result_array();         
		// return array();
        }
     }
     
     //获取推荐列表
     
     /*
      * 获取转发列表
      * 
      * 
      */
     function reposttuis($uid,$p,$pe,$city,$psize=4,$showtype=0)
    {
        $gender = array('n'=>0,'f'=>1,'m'=>2);
        if(empty($uid)) return '登录超时，请重新登录';
        if(!is_numeric($p)) return '参数错误！1';
        if(!empty($pe)){
            if(!is_numeric($pe)) return '参数错误！2';
        } 
        if(!empty($city)){
           if(!is_numeric($city)) return '参数错误！3';
        } 
        if(!empty($showtype)){
            if(!is_numeric($showtype)) return '参数错误！4';
            //if(!isset($uid['isgoogd'])){
                //$uid['isgood'] = $showtype;
           // }
           if($showtype==1)
           {
               $uid['isgood'] =1; 
           }
        }
        $chkid = $this->_getpostid($uid['wb_id'], 2);
        $this->db->select('a.id,a.uid,a.status_id,a.wb_text,a.thumbnail_pic,a.rp_points,a.r_text,a.reasons_json,a.r_pic,a.r_uid,a.r_name,a.datetime,b.screen_name,b.profile_image_url',FALSE);
        $this->db->from('wtt_repost as a');
        $this->db->join('wtt_customers b','a.uid = b.wb_id','left');
        if($pe != '0' && !empty($pe)) $this->db->where('b.province',$pe);
        if($city != '0'  && !empty($city)) $this->db->where('b.city',$city);
        $this->db->where('(a.rp_fans = 0 or a.rp_fans <= '.$uid["followers_count"].')');
        $this->db->where('(a.r_gender = 0 or a.r_gender='.$gender[$uid['gender']].')');
        //$this->db->where('(a.rp_max_num = 0 or a.rp_max_num > a.rp_num)');
        //$this->db->where('a.uid !='.$uid['wb_id']);
        $this->db->where('a.rp_status',1);
        
        if(!empty($chkid)){
            $this->db->where('a.id  NOT IN('.implode(',',$chkid).')');
           
        }
        if(isset($uid['isgood']) && $uid['isgood'] =='1'){
            $this->db->where('a.isgood',1);
        }
        $this->db->order_by('a.rp_points desc');
        $num = ($p-1)*$psize;
        //echo $num;
        $this->db->limit($psize,$num);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }else{
            return array();
        }
     }
     /*取转发列表
     * $uid 用户id
     *$p 省份id
     *$city 城市id
     * $followers_count 用户粉丝数
     */
     function tuoguangnum($uid,$pe,$city,$gender,$followers_count,$type=0)
     {
         if($type == '0'){
            $genderd = array('n'=>0,'f'=>1,'m'=>2);
            $this->db->where('(follow_gender = 0 or follow_gender='.$genderd[$gender].')');
            $this->db->where('(follow_fans = 0 or follow_fans>'.$followers_count.')');
            $this->db->where('(follow_max_num = 0 or follow_max_num > follow_num)');
            $this->db->where('uid !='.$uid);
            $this->db->where('follow_status',1);
            return $this->db->count_all_results('wtt_follow');
         }else if($type == '1'){
            $this->db->where('(rp_fans = 0 or rp_fans>'.$followers_count.')');
            $this->db->where('(rp_max_num = 0 or rp_max_num > rp_num)');
            $this->db->where('uid !='.$uid);
            $this->db->where('rp_status',1);
            return $this->db->count_all_results('wtt_repost');
         }else{
             return 0;
         }
        
     }
     //设置关注
     function dofollow($uid,$touid,$h_limit=0,$d_limit=0)
     {
        if(empty($uid)) return '登录超时，请重新登录';
        if(!is_numeric($touid)) return '参数错误！';
        $toinfo = $this->get($touid);
        if(empty($toinfo)) return '获取被关注列表错误';
        if($toinfo['follow_status'] == '0') return '此关注已经暂停';
        $xjif = $toinfo['follow_points'];
        $this->load->model('Mwbuser');
        $toinfo1 = $this->Mwbuser->getinfo($touid,"jifen");
        if(empty($toinfo1)) return "读取用户资料错误";
        //echo $toinfo1['jifen'];
        if($xjif > $toinfo1['jifen']){ 
            $this->setactive($touid, 0, 0, 0);
            return "被关注者积分不足";
        }
        $r_msg = $this->chknum($touid,0,0);
        if(!empty($r_msg))
        {
            $this->setactive($touid,0,0,0);
            return $r_msg;
        }
        if($this->Mwbuser->chkpay($uid,$touid,0,1) > 0) return '您已经操作过，不可多次操作';
        if(!$this->_chkpostnum($uid, 1, $h_limit, $d_limit)) return '关注次数超过限制';
        return '';
        
        
        //$this->sinaapi->access_token = $this->sen_data['access_token'];
        //$wb_c = $this->sinaapi->follow_by_id($touid);
     }
    
     /*
      * 
      */
    function dorepost($uid,$status_id,$reason,$is_comment,$h_limit=0,$d_limit=0)
    {
        if(empty($uid)) return '登录超时，请重新登录';
        if(!is_numeric($status_id)) return '参数错误！'.$status_id;
        //$reason = html_escape($reason);
        if(!in_array($is_comment,array('0','1'))) return '参数错误！';
        $toinfo = $this->get($status_id,1);
        if(empty($toinfo)) return '获取推广内容错误！';
        if($toinfo['rp_status'] != '1') return '此推广已经暂停！';
        $this->load->model('Mwbuser');
        if($this->Mwbuser->chkpay($uid,0,$status_id,2) > 0) return '您已经操作过，不可多次操作';
        $rp_points = $toinfo['rp_points'];
		
        $toinfo1 = $this->Mwbuser->getinfo($toinfo['uid'],"jifen");
        if(empty($toinfo1)) return "读取用户资料错误";
        //echo $toinfo1['jifen'];
		
        if($rp_points > $toinfo1['jifen']){ 
            $this->setactive($toinfo['uid'], $toinfo['id'], 1, 0);
            return "被转发者积分不足";
        }
        $r_msg = $this->chknum($toinfo['uid'],$status_id,1);
        if(!empty($r_msg))
        {
            $this->setactive($toinfo['uid'],$status_id,1,0);
            return $r_msg;
        }
        if(!$this->_chkpostnum($uid, 2, $h_limit,$d_limit)) return '转发次数超过限制';
       // $touid = $toinfo['touid'];
       // $r_wb_id = $toinfo['status_id'];
        
       return '';
        
    }
    
   /*
    * 写入已经关注的信息
    */
    function insert_hasfls($uid,$touid)
    {
        $DB1 = $this->load->database('update', TRUE);
        $date = array(
            'uid'=> $uid,
            'touid' => $touid,
            'datatime' => time()
        );
        $DB1->insert('wtt_hasfollows',$date);
        return true;
    }
    private function _gettodaynum($uid){
        $this->db->from('wtt_repost');
        $this->db->where('uid',$uid);
        return $this->db->count_all_results();
    }
    private function _chkfloow(){
        $info = '';
        //$this->_chekref() ? $info = '' :  $info = '不合法的提交信息！'; 
        if(! empty($info)) return $info; 
        $follow_points = intval($this->uri->segment(3)); //积分
        $follow_gender = $this->uri->segment(5);//性别
        $follow_fans = intval($this->uri->segment(6));//粉丝
        $follow_max_num = intval($this->uri->segment(4));//最大数
        if(!in_array($follow_gender,array('0','1','2'))){
           return  '性别限制选择错误！';  
        }
        if( ! is_int($follow_points)) return '出分必须是正整数！';
        if($follow_points < 2 || $follow_points > 99 )            return '出分必须大于1，小于99！';
        if(!is_int($follow_max_num)) return '最大次数必须是正整数！';
        if(!is_int($follow_fans) ) return '粉丝数限制数必须是正整数！';
        return $info;
    }
    
    
    
    function _chkpostnum($uid,$ptype,$h_limit,$d_limit)
    {
      
      $query = $this->db->get_where('wtt_chknum',array('uid'=>$uid),1);
       if($query->num_rows() >0)
       {
          $row = $query->row_array();
          $follownum = $row['follownum'];
          $rpostnum = $row['rpostnum'];
          $d_f_n = $row['d_f_n'];
          $d_r_n = $row['d_r_n'];
          $uptime = $row['uptime'];
          $f_uptime = $row['f_uptime'];
          if($ptype == '1')
          {
              return $this->chkfollow($uid,$h_limit,$d_limit,$follownum,$d_f_n,$f_uptime);
          }else{
              return $this->chkrepost($uid,$h_limit,$d_limit,$rpostnum,$d_r_n,$uptime);
          }
           return TRUE;
       }else{
           if($ptype == '1')
           {
             $follownum=1 ;
             $d_f_n=1;
             $rpostnum = 0;
             $d_r_n = 0;
           }else{
             $rpostnum=1;  
             $d_r_n=1;
             $follownum=0 ;
             $d_f_n=0;
           }  
           $data = array(
               'uid'=>$uid,
               'follownum' =>$follownum,
               'rpostnum' =>$rpostnum,
               'd_f_n' =>$d_f_n,
               'd_r_n' =>$d_r_n,
               'uptime'=>  time(),
               'f_uptime' => time()
           );
           $DB1 = $this->load->database('update', TRUE);
           $DB1->insert('wtt_chknum',$data);
           return TRUE;
       }
    }
    
    private function chkfollow($uid,$h_limit,$d_limit,$follownum,$d_f_n,$uptime)
    {
        $DB1 = $this->load->database('update', TRUE);
        if(date('Y-n-j',$uptime) != date('Y-n-j',time()))
        {
            
            $DB1->query('update wtt_chknum set follownum=1,d_f_n=1,f_uptime='.time().' where uid='.$uid);
            return true;  
        }else{
            if($d_limit <= $d_f_n) return false;
            if(date('Y-n-j H',$uptime) != date('Y-n-j H',time())){
              $DB1->query('update wtt_chknum set follownum=1,d_f_n='.($d_f_n+1).',f_uptime='.time().' where uid='.$uid);  
            }else{
              if($h_limit <= $follownum) 
              {
                  return false;              
              }else{
                  $DB1->query('update wtt_chknum set follownum='.($follownum+1).',d_f_n='.($d_f_n+1).',f_uptime='.time().' where uid='.$uid);
              }  
            }
            return true;
        }
    }
    private function chkrepost($uid,$h_limit,$d_limit,$rpostnum,$d_r_n,$uptime)
    {
         $DB1 = $this->load->database('update', TRUE);
        if(date('Y-n-j',$uptime) != date('Y-n-j',time()))
        {
            
            $DB1->query('update wtt_chknum set rpostnum=1,d_r_n=1,uptime='.time().' where uid='.$uid);
            return true;  
        }else{
            if($d_limit <= $d_r_n) return false;
            if(date('Y-n-j H',$uptime) != date('Y-n-j H',time())){
              $DB1->query('update wtt_chknum set rpostnum=1,d_r_n='.($d_r_n+1).',uptime='.time().' where uid='.$uid);  
            }else{
              if($h_limit <= $rpostnum) 
              {
                  return false;              
              }else{
                  $DB1->query('update wtt_chknum set rpostnum='.($rpostnum+1).',d_r_n='.($d_r_n+1).',uptime='.time().' where uid='.$uid);
              }  
            }
            return true;
        }
    }
    private function _getpostid($uid,$ptype)
    {
        $rearr = array();
         if($ptype == '1')
        {
            $this->db->select('touid');
            $this->db->where('ptype',1);
        }else{
            $this->db->select('status_id');
            $this->db->where('ptype',2);
        }
        $this->db->where('uid',$uid);
        $this->db->from('wtt_chkpay');
        $query = $this->db->get();
        if($query->num_rows() > 0)
        {
            $i = 0; 
            foreach ($query->result_array() as $row)
            {
               if($ptype=='1'){
                    $rearr[$i] =  $row['touid'];
               }else{
                    $rearr[$i] =  $row['status_id'];
               }
               $i++;
            }
        
        }
        if($ptype == 1){
            $j = sizeof ($rearr);
            $query = $this->db->query('select touid from wtt_hasfollows where uid='.$uid);
            if($query->num_rows()>0)
            {
                foreach ($query->result_array() as $row)
                {
             
                      $rearr[$j] =  $row['touid'];
                       $j++;
                    }
                }
            
        }
        
        
        return $rearr;
    }
    
}

